/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#include "powerlawlearningrate.hh"

#include <boost/optional.hpp>
#include <boost/none.hpp>
#include <stdexcept>

namespace
{
  using namespace flassol;

  boost::optional<double>
  maybe_parse_double (const std::string& s)
    {
      double rv;
      char* endptr;

      rv = strtod (s.c_str (), &endptr);

      if (s.c_str ()[0] != '\0' && *endptr == '\0')
        {
          return rv;
        }

      return boost::none;
    }

  const char*
  help ()
    {
      return "specify as 'powerlaw (initial_rate, initial_t = 1, power_t = 0.5, offset_t = 0)', which indicates eta (t) = (initial_rate * pow (initial_t, power_t) / pow (initial_t + offset_t + t, power_t)); where initial_rate > 0, initial_t > 0, power_t >= 0; C++ default argument conventions apply but initial_rate must be specified.";
    }

  LearningRate*
  make_power_law_learning_rate (const std::string& s)
    {
      std::string::size_type openparen = s.find_first_of ("(", 0);

      if (openparen == std::string::npos)
        {
          boost::optional<double> number = maybe_parse_double (s);

          if (! number)
            {
              throw std::invalid_argument (help ());
            }

          return new PowerLawLearningRate (*number);
        }
      else
        {
          const char* c = s.c_str () + openparen + 1;
          char* endptr;
          LearningRate* rv = 0;
          double initial_rate;

          initial_rate = strtof (c, &endptr);

          if (initial_rate > 0.0f)
            {
              if (*endptr == ')')
                {
                  rv = new PowerLawLearningRate (initial_rate);
                }
              else if (*endptr == ',')
                {
                  double initial_t;
    
                  initial_t = strtof (endptr + 1, &endptr);

                  if (initial_t > 0.0f)
                    {
                      if (*endptr == ')')
                        {
                          rv = new PowerLawLearningRate (initial_rate,
                                                         initial_t);
                        }
                      else if (*endptr == ',')
                        {
                          double power_t;

                          power_t = strtof (endptr + 1, &endptr);

                          if (power_t >= 0.0f)
                            {
                              if (*endptr == ')')
                                {
                                  rv = new PowerLawLearningRate (initial_rate,
                                                                 initial_t,
                                                                 power_t);
                                }
                              else if (*endptr == ',')
                                {
                                  double offset_t;

                                  offset_t = strtof (endptr + 1, &endptr);

                                  if (offset_t >= 0.0f)
                                    {
                                      if (*endptr == ')')
                                        {
                                          rv = 
                                            new PowerLawLearningRate 
                                              (initial_rate,
                                               initial_t,
                                               power_t,
                                               offset_t);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

          if (! rv)
            {
              throw std::invalid_argument (help ());
            }

          return rv;
        }
    }
}

namespace flassol
{
  LearningRateFactory PowerLawLearningRate::factory = 
    register_learning_rate_factory ("powerlaw",
                                    make_power_law_learning_rate);
}
